package com.basic.codetool;

import java.io.StringWriter;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;

import com.basic.common.FuncFile; 
 
/**
 * 根据数据库中表生成对应的文件(info文件，query文件，maptable文件)
 * @author lh
 *
 */
public class CreateFile {
	public static String foldName = null;
	public static String encode = "UTF-8";
	static {
		Velocity.addProperty(Velocity.ENCODING_DEFAULT, encode);
		Velocity.addProperty(Velocity.INPUT_ENCODING, encode);
		Velocity.addProperty(Velocity.OUTPUT_ENCODING, encode); 
		Velocity.addProperty(Velocity.FILE_RESOURCE_LOADER_PATH, CreateFile.class.getResource("").toString()
				.replaceAll("^file:/", ""));
		Velocity.init();
	}
	public static void main(String[] args) {
		
		//配置表
		String tableName = "ma_data";
		foldName = "E:/project/newstock/src/main/java/com/dataface/";
		
		
		//程序开始
		CreateFile cmt = new CreateFile();
		CoodToolDbConnect db = CoodToolDbConnect.instance();
		TableInfo table = new TableInfo();
		table.setPackagePath("com.dataface");
		table.setTableName(tableName);
		List<String> fields = db.allFields(tableName);
		for(String field : fields){
			table.putFields(FieldInfo.instance(field));
		}
		table.setKeyField(db.getKeyFieldName(tableName));
		cmt.createQuery(table);
		cmt.createRowdata(table);
		cmt.createMapTable(table);
	}
	/**
	 * if table is null
	 * 
	 * @param table
	 */
	public void createMapTable(TableInfo table) {
		String packageName = table.getPackagePath()+".maptable";
		 
		Context context = new VelocityContext();
		context.put("table", table);
		context.put("package", packageName);
		StringWriter sw = new StringWriter();
		Velocity.mergeTemplate("MapTable.vm", encode, context, sw);
		FuncFile.insertFile(this.foldName+"maptable/T_"+table.getTableNameUp()+".java", sw.toString());
		System.out.println(this.foldName+"maptable/T_"+table.getTableNameUp()+".java : complete! " );
 	}

	public void createRowdata(TableInfo table){
		String packageName = table.getPackagePath()+".rowdata";
		 
		Context context = new VelocityContext();
		context.put("table", table);
		context.put("package", packageName);
		StringWriter sw = new StringWriter();
		Velocity.mergeTemplate("Rowdata.vm", encode, context, sw);
		FuncFile.insertFile(this.foldName+"rowdata/"+table.getTableNameHeadUp()+"RowInfo.java", sw.toString());
		System.out.println(this.foldName+"rowdata/"+table.getTableNameHeadUp()+"RowInfo.java : complete! " );
	}
	
	public void createQuery(TableInfo table){
		String packageName = table.getPackagePath()+".query";
		 
		Context context = new VelocityContext();
		context.put("table", table);
		context.put("package", packageName);
		StringWriter sw = new StringWriter();
		Velocity.mergeTemplate("Query.vm", encode, context, sw);
		FuncFile.insertFile(this.foldName+"query/"+table.getTableNameHeadUp()+"Query.java", sw.toString());
		System.out.println(this.foldName+"query/"+table.getTableNameHeadUp()+"Query.java : complete! " );
	}
	
	
}
 